/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.appweb;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.cache.JedisUtils;
import com.jeesite.common.responseReturn.entity.ResultObject;
import com.jeesite.common.responseReturn.utils.CommonUtils;
import com.jeesite.common.responseReturn.web.Render;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.goods.entity.ZtComment;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.entity.ZtGoodsOrder;
import com.jeesite.modules.goods.service.ZtCommentService;
import com.jeesite.modules.goods.service.ZtGoodsOrderService;
import com.jeesite.modules.xcxAdmin.entity.ZtAdmin;
import com.jeesite.modules.xcxAdmin.entity.ZtGoodsqcode;
import com.jeesite.modules.xcxAdmin.service.ZtGoodsqcodeService;

/**
 * 对商品评论Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${frontPath}/ztComment")
public class AppZtCommentController extends BaseController {

	@Autowired
	private ZtCommentService ztCommentService;
	@Autowired
	ZtGoodsOrderService ztGoodsOrderService;
	@Autowired
	ZtGoodsqcodeService ztGoodsqcodeService;

	

	/**
	 * 查询列表数据
	 */
	
	@RequestMapping(value = "listData")
	@ResponseBody
	public void listData(HttpServletRequest request,
			HttpServletResponse response) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		String goodsid = jo.getString("goodsid");
		String adminid = jo.getString("adminid");
		String pageNum = "1";
		
		if(jo.get("pageNum")!=null){
			pageNum=jo.get("pageNum").toString();
		}
		
		ZtComment ztComment=new ZtComment();
		
		ztComment.setPageNum((Integer.valueOf(pageNum) - 1) * 15);
		ZtGoods ztGoods=new ZtGoods();
		
		
		ztGoods.setId(goodsid);
		ztComment.setZtGoods(ztGoods);
		
		List<ZtComment> ztCommentList = ztCommentService.findMh(ztComment);
		for(ZtComment zc:ztCommentList){
			String sign=JedisUtils.get("c"+adminid+zc.getId());
			if(StringUtils.isNotBlank(sign)&& "1".equals(sign)){
				zc.setZanSign("1");
			}else{
				zc.setZanSign("0");
			}
		}

		res.setResult(200L);
		res.setData(ztCommentList);
		Render.renderString(response, res);
	}
	
	@PostMapping(value = "zan")
	@ResponseBody
	public void zan(HttpServletRequest request,
			HttpServletResponse response) {
		String paramStr = CommonUtils.getParams(request);
		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		String commentid = jo.getString("commentid");
		String adminid = jo.getString("adminid");
		
		ZtComment ztComment=new ZtComment();
		ztComment.setId(commentid);
		ztComment=ztCommentService.get(ztComment);
		
		String sign=JedisUtils.get("c"+adminid+ztComment.getId());
		if(StringUtils.isNotBlank(sign)&& "1".equals(sign)){//已经点赞了  改为没有点赞
			JedisUtils.set("c"+adminid+ztComment.getId(),"0",0);
			if(ztComment.getZan()!=null && ztComment.getZan()>=1){
			ztComment.setZan(ztComment.getZan()-1);
			}
			
		}else{
			JedisUtils.set("c"+adminid+ztComment.getId(),"1",0);
			ztComment.setZan(ztComment.getZan()==null?0:ztComment.getZan()+1);
		}
		ztCommentService.save(ztComment);
		res.setResult(200L);
		res.setData(ztComment);
		Render.renderString(response, res);
	}
	
	/**
	 * 保存数据
	 */
	
	@PostMapping(value = "save")
	@ResponseBody
	public void save(HttpServletRequest request,
			HttpServletResponse response) {
		String paramStr = CommonUtils.getParams(request);

		JSONObject jo = JSONObject.parseObject(paramStr);
		ResultObject res = new ResultObject();
		String goodsid = jo.getString("goodsid");
		String adminid = jo.getString("adminid");
		
		String content = jo.getString("content");
		String imgs = jo.getString("imgs");
		
		ZtComment ztComment=new ZtComment();
		
		ZtGoods ztGoods=new ZtGoods();
		ztGoods.setId(goodsid);
		ZtAdmin ztAdmin=new ZtAdmin();
		ztAdmin.setId(adminid);
		ztComment.setZtGoods(ztGoods);
		ztComment.setZtAdmin(ztAdmin);
		ztComment.setContent(content);
		ztComment.setImgs(imgs);
		ztComment.setZan(0);
		ztComment.setCreateDate(new Date());
		
		ztCommentService.save(ztComment);
		
		res.setResult(200L);
		res.setData(ztComment);
		Render.renderString(response, res);
	}
	
	
}